package com.dalin.platform.loa.model;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.dalin.common.base.BaseObject;
import com.dalin.platform.loa.condition.LoaRoleOperateCondition;
import com.dalin.platform.pub.PlatContext;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-05-11                                                     *
 * @modDate: 2010-05-11                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
@Entity
@Table(name="LOA_ROLE")
public class LoaRoleEntity extends BaseObject {
    private Long roleId; // ROLE_ID,16,0
    private Long projectId; // PROJECT_ID,16,0
    private Integer roleValue; // ROLE_VALUE,8,0
	private String roleName; // ROLE_NAME,256,0
    private String roleDescription; // ROLE_DESCRIPTION,512,0
    private Date createDate; // CREATE_DATE,7,0
    private Date modifyDate; // MODIFY_DATE,7,0
    
    private Set<LoaMenuEntity> loaMenuEntitys = new HashSet<LoaMenuEntity>(0);
    private Set<LoaUserEntity> loaUserEntitys = new HashSet<LoaUserEntity>(0);

    @Id
    @Column(name="ROLE_ID")
    @GeneratedValue(strategy = GenerationType.TABLE,generator="generator_normal")
    @TableGenerator(
        name = "generator_normal",
        table = "GLB_GENERATOR_KEY",
        pkColumnName = "GEN_KEY",
        valueColumnName = "GEN_VALUE",
        pkColumnValue = "LOA_ROLE",
        allocationSize=1
    )
    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }
    @Column(name="PROJECT_ID")
    public Long getProjectId() {
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	@Column(name="ROLE_VALUE")
	public Integer getRoleValue() {
		return roleValue;
	}

	public void setRoleValue(Integer roleValue) {
		this.roleValue = roleValue;
	}
	
    @Column(name="ROLE_NAME")
    public String getRoleName() {
        return roleName;
    }

	public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Column(name="ROLE_DESCRIPTION")
    public String getRoleDescription() {
        return roleDescription;
    }

    public void setRoleDescription(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CREATE_DATE")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="MODIFY_DATE")
    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name = "LOA_ROLE_MENU", joinColumns = {
	        @JoinColumn(name = "ROLE_ID")
	    }, inverseJoinColumns =  {
	        @JoinColumn(name = "MENU_ID")
	    }
    )
	public Set<LoaMenuEntity> getLoaMenuEntitys() {
		return loaMenuEntitys;
	}

	public void setLoaMenuEntitys(Set<LoaMenuEntity> loaMenuEntitys) {
		this.loaMenuEntitys = loaMenuEntitys;
	}
	
	
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name = "LOA_ROLE_USER", joinColumns = {
	        @JoinColumn(name = "ROLE_ID")
	    }, inverseJoinColumns =  {
	        @JoinColumn(name = "USER_ID")
	    }
    )
	public Set<LoaUserEntity> getLoaUserEntitys() {
		return loaUserEntitys;
	}

	public void setLoaUserEntitys(Set<LoaUserEntity> loaUserEntitys) {
		this.loaUserEntitys = loaUserEntitys;
	}
	
	@Transient
    public LoaProjectEntity getLoaProjectEntity() throws Exception {
		return (LoaProjectEntity) PlatContext.getDao()
				.findObjById(LoaProjectEntity.class, this.getProjectId());
	}
	
	@Transient
    public List<LoaRoleOperateEntity> getLoaRoleOperateEntitys() throws Exception {
    	LoaRoleOperateCondition lroCond = new LoaRoleOperateCondition();
		lroCond.setRoleId(roleId);
		return PlatContext.getDao().queryObjsToPages(lroCond.prepareParams()
				.getPreparedPageQuery());
	}
	
	
    @Override
    public int hashCode() {
        int njlrst = 17;
        njlrst = 31 * njlrst + (roleId == null ? 0 : roleId.hashCode());
        njlrst = 31 * njlrst + (projectId == null ? 0 : projectId.hashCode());
        njlrst = 31 * njlrst + (roleValue == null ? 0 : roleValue.hashCode());
        njlrst = 31 * njlrst + (roleName == null ? 0 : roleName.hashCode());
        njlrst = 31 * njlrst + (roleDescription == null ? 0 : roleDescription.hashCode());
        njlrst = 31 * njlrst + (createDate == null ? 0 : createDate.hashCode());
        njlrst = 31 * njlrst + (modifyDate == null ? 0 : modifyDate.hashCode());
        return njlrst;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LoaRoleEntity)) return false;
        final LoaRoleEntity other = (LoaRoleEntity) o;
        if (roleId == null) {
            if (other.roleId != null) return false;
        } else if (!roleId.equals(other.roleId)) {
            return false;
        }
        if (projectId == null) {
            if (other.projectId != null) return false;
        } else if (!projectId.equals(other.projectId)) {
            return false;
        }
        if (roleValue == null) {
            if (other.roleValue != null) return false;
        } else if (!roleValue.equals(other.roleValue)) {
            return false;
        }
        if (roleName == null) {
            if (other.roleName != null) return false;
        } else if (!roleName.equals(other.roleName)) {
            return false;
        }
        if (roleDescription == null) {
            if (other.roleDescription != null) return false;
        } else if (!roleDescription.equals(other.roleDescription)) {
            return false;
        }
        if (createDate == null) {
            if (other.createDate != null) return false;
        } else if (!createDate.equals(other.createDate)) {
            return false;
        }
        if (modifyDate == null) {
            if (other.modifyDate != null) return false;
        } else if (!modifyDate.equals(other.modifyDate)) {
            return false;
        }
        if (loaMenuEntitys == null) {
            if (other.loaMenuEntitys != null) return false;
        } else if (!loaMenuEntitys.equals(other.loaMenuEntitys)) {
            return false;
        }
        if (loaUserEntitys == null) {
            if (other.loaUserEntitys != null) return false;
        } else if (!loaUserEntitys.equals(other.loaUserEntitys)) {
            return false;
        }
        return true;
    }
}
